/**
 * 
 */
package org.mspring.mlog.web.module.web.ucenter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.service.security.UserService;
import org.mspring.mlog.support.resolver.QueryParam;
import org.mspring.mlog.support.resolver.SpaceUser;
import org.mspring.mlog.web.query.PostQueryCriterion;
import org.mspring.platform.persistence.support.Page;
import org.mspring.platform.persistence.support.Sort;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Gao Youbo
 * @since 2013-6-7
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/u/{username}")
public class UCenter_IndexWidget extends UCenter_AbstractWidget {

    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    @SuppressWarnings("unchecked")
    @RequestMapping({ "/", "" })
    public String index(@SpaceUser(pathVariable = "username") User user, @ModelAttribute Post post, @QueryParam Map<String, Object> queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (user == null) {
            return "skin:/404";
        }

        // 处理参数
        Integer page = RequestUtils.getRequestParameterAsInteger(request, "page");
        Long catalog = RequestUtils.getRequestParameterAsLong(request, "catalog");

        if (page == null) {
            page = 1;
        }
        Page<Post> postPage = new Page<Post>();
        if (queryParams == null) {
            queryParams = new HashMap<String, Object>();
        }
        postPage.setPageSize(1);
        postPage.setPageNo(page);
        postPage.setSort(new Sort("id", Sort.DESC));
        queryParams.put("author.id", user.getId());
        queryParams.put("status", Post.Status.PUBLISH);
        if (catalog != null) {
            queryParams.put("catalog.id", catalog);
        }
        postPage = postService.listPage(new PostQueryCriterion(queryParams), postPage);
        model.addAttribute("postPage", postPage);
        return "skin:/ucenter/index";
    }

    @RequestMapping("/post")
    public String post(@SpaceUser(pathVariable = "username") User user, @RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return "skin:/ucenter/404";
        }
        Post post = postService.get(id);
        if (post != null && Post.Status.PUBLISH.equals(post.getStatus()) && post.getAuthor() != null && post.getAuthor().getId() == user.getId()) {
            model.addAttribute("post", post);
        }
        return "skin:/ucenter/post";
    }
}
